package com.originskyseed.platform.user.controller;

import java.util.Arrays;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.common.annotation.SysLog;
import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.core.util.bean.security.MD5Util;
import com.originskyseed.platform.user.i18n.UserResponseMsg;
import com.originskyseed.platform.user.model.TradeUnion;
import com.originskyseed.platform.user.model.UserInfo;
import com.originskyseed.platform.user.service.ITradeUnionService;
import com.originskyseed.platform.user.service.IUserInfoService;
import com.originskyseed.platform.user.vo.request.TradeUnionRequestVO;

/**
 * 公会信息Controller类（OSI_TRADE_UNION）
 *
 * @author tangwanghui
 */
@Controller
@RequestMapping("/tradeUnion")
@BaseLocaleResource(localeFileName = "i18n_user")
public class TradeUnionController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(TradeUnionController.class);

	@DubboReference
	private ITradeUnionService tradeUnionService;

	@DubboReference
	private IUserInfoService userInfoService;

	/**
	 * 分页查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/tradeUnionList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<TradeUnion>> tradeUnionList(HttpServletRequest request) {
		BaseResponseBody<BasePage<TradeUnion>> result = null;
		try {
			// 请求对象
			TradeUnionRequestVO requestVO = this.getParameterBean(TradeUnionRequestVO.class);

			// 分页查询
			BasePage<TradeUnion> page = new BasePage<TradeUnion>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.tradeUnionService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<TradeUnion>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<TradeUnion>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 开放平台选择公会列表 分页查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectTradeUnionList.json")
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<TradeUnion>> selectTradeUnionList(HttpServletRequest request) {
		BaseResponseBody<BasePage<TradeUnion>> result = null;
		try {
			// 请求对象
			TradeUnionRequestVO requestVO = this.getParameterBean(TradeUnionRequestVO.class);
			// 分页查询
			BasePage<TradeUnion> page = new BasePage<TradeUnion>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.tradeUnionService.selectTradeUnionList(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<TradeUnion>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<TradeUnion>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/tradeUnionSave.json", method = RequestMethod.POST)
	public BaseResponseBody<TradeUnion> tradeUnionSave(HttpServletRequest request) {
		BaseResponseBody<TradeUnion> result = null;
		try {
			// 请求对象
			TradeUnion tradeUnion = this.getParameterBean(TradeUnion.class);

			if (StringUtils.isNotBlank(tradeUnion.getId())) {
				this.tradeUnionService.updateByPrimaryKey(tradeUnion);
			} else {
				this.tradeUnionService.insertSelective(tradeUnion);
			}
			result = new BaseResponseBody<TradeUnion>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_SUCCESS.getMsg()), tradeUnion);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<TradeUnion>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/tradeUnionDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> tradeUnionDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});

			this.tradeUnionService.deleteByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 审核公会接口
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/updateTradeUnionStatus.json", method = RequestMethod.POST)
	public BaseResponseBody updateTradeUnionStatus(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 当前用户
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			// 请求对象
			String id = this.getParameterString("id", null);
			Integer auditStatus = this.getParameterInteger("auditStatus", null);
			String auditRemark = this.getParameterString("auditRemark", null);

			if (StringUtils.isNotEmpty(id)) {
				TradeUnion tradeUnion = new TradeUnion();
				tradeUnion.setId(id);
				tradeUnion.setAuditStatus(auditStatus);
				tradeUnion.setAuditRemark(auditRemark);
				tradeUnion.setAuditTime(new Date());
				tradeUnion.setAuditUserAccount(userInfo.getAccount());
				tradeUnion.setAuditUserName(userInfo.getName());
				this.tradeUnionService.updateByPrimaryKeySelective(tradeUnion);
			}
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存修改密码
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/userInfoEditPassword.json")
	@RequiredParams(names = { "password", "newPassword", "validatePassword" })
	@SysLog(module = "用户模块", operationDesc = "修改用户密码")
	public BaseResponseBody userInfoValidatePassword(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求对象
			UserInfo currentUserInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			String id = currentUserInfo.getId();
			// 原密码
			String password = this.getParameterString("password", null);
			// 新密码
			String newPassword = this.getParameterString("newPassword", null);
			// 重复输入的验证密码
			String validatePassword = this.getParameterString("validatePassword", null);
			UserInfo userInfo = this.userInfoService.selectByPrimaryKey(id);
			// 两次输入的密码不一样
			if (!newPassword.equals(validatePassword)) {
				result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.PASSWORD_NOT_SAME_ERROR.getMsg()), null);
			} else {
				// 判断原密码是否正确
				if (!MD5Util.encrypt(password).equals(userInfo.getPassword())) {
					result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.OLD_PASSWORD_ERROR.getMsg()), null);
				} else if (MD5Util.encrypt(newPassword).equals(userInfo.getPassword())) {
					result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.OLD_PASS_NEW_PASS_AGREEMENT_ERROR.getMsg()), null);
				} else {
					// 替换密码
					userInfo.setPassword(MD5Util.encrypt(newPassword));
					userInfo.setUpdatePassTime(new Date());
					this.userInfoService.updateByPrimaryKeySelective(userInfo);
					result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_SUCCESS.getMsg()), true);
				}
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存修改权限密码
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/userInfoEditPermissionPassword.json")
	@RequiredParams(names = { "permissionPassword", "newPassword", "validatePassword" })
	@SysLog(module = "用户模块", operationDesc = "修改用户密码")
	public BaseResponseBody userInfoEditPermissionPassword(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求对象
			UserInfo currentUserInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			String id = currentUserInfo.getId();
			// 原密码
			String permissionPassword = this.getParameterString("permissionPassword", null);
			// 新密码
			String newPassword = this.getParameterString("newPassword", null);
			// 重复输入的验证密码
			String validatePassword = this.getParameterString("validatePassword", null);
			UserInfo userInfo = this.userInfoService.selectByPrimaryKey(id);
			// 两次输入的密码不一样
			if (!newPassword.equals(validatePassword)) {
				result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.PASSWORD_NOT_SAME_ERROR.getMsg()), null);
			} else {
				// 判断原密码是否正确
				if (!MD5Util.encrypt(permissionPassword).equals(userInfo.getPermissionPassword())) {
					result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.OLD_PASSWORD_ERROR.getMsg()), null);
				} else if (MD5Util.encrypt(newPassword).equals(userInfo.getPermissionPassword())) {
					result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.OLD_PASS_NEW_PASS_AGREEMENT_ERROR.getMsg()), null);
				} else {
					// 替换密码
					userInfo.setPermissionPassword(MD5Util.encrypt(newPassword));
					userInfo.setUpdatePassTime(new Date());
					this.userInfoService.updateByPrimaryKeySelective(userInfo);
					result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_SUCCESS.getMsg()), true);
				}
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}
}
